package com.woparker.kiosk.anim;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

import com.woparker.kiosk.bean.PathPoint;


/**
 * Created by yuanxd on 2017/12/1 0001.
 * 路线图标运动
 */

public class IconAnimator {

    //运动图标
    private ImageView icon;
    //图标宽度
    public int icon_width = 25;
    //图标高度
    public int icon_height = 16;
    //属性动画
    private ObjectAnimator anim;

    /**
     * 构造方法
     *
     * @param icon 图片对象
     */
    public IconAnimator(ImageView icon) {
        this.icon = icon;
        icon_width = this.icon.getWidth();
        icon_height = this.icon.getHeight();
    }


    /**
     * 设置View的属性通过ObjectAnimator.ofObject()的反射机制来调用
     * 自动映射实现所以属性名必须一致（如:fab）
     * @param newLoc
     */
    public void setFab(PathPoint newLoc) {
        icon.setTranslationX(newLoc.mX);
        icon.setTranslationY(newLoc.mY);
    }

    /**
     * 重复运动动画
     *
     * @param level        地图缩放级别
     * @param animatorPath 属性动画集合
     */
    public void startAnimator(float level, AnimatorPath animatorPath) {
        anim = ObjectAnimator.ofObject(this, "fab", new PathEvaluator(), animatorPath.getPoints().toArray());
        anim.setInterpolator(new DecelerateInterpolator());
        anim.setRepeatCount(ValueAnimator.INFINITE);//无限循环
        anim.setDuration((int) (level * 3000));
        anim.start();
    }


    /**
     * 将icon移动到对应的位置
     *
     * @param x
     * @param y
     */
    public void viewMovePoint(final float x, final float y) {
        icon.post(new Runnable() {
            @Override
            public void run() {
                icon.setTranslationX(x);
                icon.setTranslationY(y);
            }
        });


    }


    /**
     * 停止动画
     */
    public void cancleAnimator(){
        if(null != anim) {
            anim.end();
            anim.cancel();
        }
    }

}
